package com.slm.report.state;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.StringUtils;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction;
import com.slm.mylibrary.base.BaseViewModel;
import com.slm.report.db.StudentInfoBean;
import com.slm.report.db.StudentInfoBean_Table;

import java.util.List;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.state
 * @ClassName: HomePageViewModule
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/5/29 16:36
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/29 16:36
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class HomePageViewModel extends BaseViewModel {


    public final MutableLiveData<List<StudentInfoBean>> list = new MutableLiveData<>();


    public void obtainStudentInfos(String searchKey) {
        if (StringUtils.isEmpty(searchKey)) {
            SQLite.select()
                    .from(StudentInfoBean.class)
                    .async().queryListResultCallback(new QueryTransaction.QueryResultListCallback<StudentInfoBean>() {
                @Override
                public void onListQueryResult(QueryTransaction transaction, @NonNull List<StudentInfoBean> tResult) {
                    list.setValue(tResult);
                }
            }).execute();

        } else {
            SQLite.select()
                    .from(StudentInfoBean.class)
                    .where(StudentInfoBean_Table.name.like("%" + searchKey + "%"))
                    .async().queryListResultCallback(new QueryTransaction.QueryResultListCallback<StudentInfoBean>() {
                @Override
                public void onListQueryResult(QueryTransaction transaction, @NonNull List<StudentInfoBean> tResult) {
                    list.setValue(tResult);
                }
            }).execute();
        }

    }


}
